package suanfa;

import java.util.Stack;

public class test7 {
    private Stack<Integer> data;
    // 辅助栈
    private Stack<Integer> helper;

    /**
     * initialize your data structure here.
     */
    public void MinStack() {
        data = new Stack<>();
        helper = new Stack<>();
    }

    public void push(int x) {
       data.push(x);
       if(helper.isEmpty())
           helper.push(x);
       else {
           if(helper.peek()>=x)
               helper.push(x);
           else
               helper.push(helper.peek());
       }
    }

    public void pop() {
        if(!data.isEmpty()){
            data.pop();
            helper.pop();
        }

    }

    public int top() {
        if(!data.isEmpty())
        return  data.peek();
        throw new RuntimeException("栈中元素为空，此操作非法");
    }

    public int getMin() {
        if (!helper.isEmpty())
        return helper.peek();
        throw new RuntimeException("栈中元素为空，此操作非法");
    }
}
